home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / win / pascal / realm.exe / REAL_MEM.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-04-12  |  3.5 KB  |  140 lines

  1.  
  2.  
  3. {Allows access to the Real mode memory (lower 1MB of physical address space).
  4. Uses the DPMI.
  5.  
  6.  
  7. Rex K. Perkins, CIS 70651,1611
  8.  
  9.  
  10. 10th April 1992}
  11.  
  12.  
  13.  
  14. Unit Real_Mem;
  15.  
  16. Interface
  17.  
  18. Uses WinProcs,WinTypes;
  19.  
  20. Type
  21.       PRealSegment=^TRealSegment;
  22.       TRealSegment=Array[0..65534] Of Byte;
  23.  
  24.  
  25.   Function GetSelector(RequiredSegment:Word):Word;
  26.  
  27.     {If we have not already got a selector, allocate one, otherwise move the
  28.     current selector to this segment. Returns the selector. No error trap}
  29.  
  30.  
  31.   Function GetRealSegment(RequiredSegment:Word):PRealSegment;
  32.  
  33.     {Get a pointer to a 64K (less 1 byte) array in real address space.
  34.      Note that any pointer obtained with this function WILL become invalid
  35.      next time function is called.}
  36.  
  37.  
  38. Implementation
  39.  
  40.  
  41. Var Selector:Word;              {The real mode selector when SelectorValid}
  42.     SelectorValid:Boolean;      {True if we have a selector}
  43.  
  44.  
  45.  
  46.   Function GetSelector(RequiredSegment:Word):Word;
  47.  
  48.   {If we have not already got a selector, allocate one, otherwise move the
  49.   current selector to this segment. Returns the selector. No error trap}
  50.  
  51.  
  52.       Function MakeDescriptor(RequiredSegment:Word):Word;
  53.  
  54.       {Use the DPMI to create a protected mode descriptor for the specified
  55.       real mode segment, and return the selector for it. Use sparingly, as the
  56.       descriptor can't be freed}
  57.  
  58.       Var Selector:Word;
  59.  
  60.       Begin
  61.         ASM
  62.             push di
  63.             push es
  64.  
  65.             mov  bx,RequiredSegment  {Pass the segment to the DPMI}
  66.             mov  ax,$0002            {DPMI function number}
  67.             int  31h                 {Call the DPMI}
  68.  
  69.                                      {If the carry flag was set, there was an error}
  70.             mov  selector,ax         {Get the selector. Not valid if CF}
  71.             pop  es
  72.             pop  di
  73.         End;
  74.         MakeDescriptor:=Selector
  75.       End;
  76.  
  77.  
  78.  
  79.       Procedure MoveSelector(RequiredSegment,Selector:Word);
  80.  
  81.       {Use the DPMI to move the mapping for the specified selector to an alternative
  82.       real mode segment.}
  83.  
  84.       Var SelectorLocal,SegmentHi,SegmentLo:Word;
  85.  
  86.       Begin
  87.         SegmentLo:=RequiredSegment SHL 4;      {Get the 32 bit base address}
  88.         SegmentHi:=RequiredSegment SHR 12;
  89.         SelectorLocal:=Selector;
  90.  
  91.         ASM
  92.             push di
  93.             push es
  94.  
  95.             mov  bx,SelectorLocal    {Selector to change}
  96.             mov  cx,SegmentHi        {High 16 bits of base address}
  97.             mov  dx,SegmentLo        {Low 16 bits of base address}
  98.             mov  ax,$0007            {DPMI function number}
  99.             int  31h                 {Call the DPMI}
  100.  
  101.                                      {If the carry flag was set, there was an error}
  102.             pop  es
  103.             pop  di
  104.         End
  105.       End;
  106.  
  107.  
  108.  
  109.  
  110.   Begin
  111.     If SelectorValid Then
  112.       MoveSelector(RequiredSegment,Selector)     {We have a selector, so move it}
  113.     Else
  114.       Begin
  115.         Selector:=MakeDescriptor(RequiredSegment);  {Make a new descriptor at the address RequiredSegment:0}
  116.         SelectorValid:=True
  117.       End;
  118.     GetSelector:=Selector
  119.   End;
  120.  
  121.  
  122.  
  123.  
  124.   Function GetRealSegment(RequiredSegment:Word):PRealSegment;
  125.  
  126.   {Get a pointer to a 64K (less 1 byte) array in real address space.
  127.    Note that any pointer obtained with this function WILL become invalid
  128.    next time function is called.}
  129.  
  130.   Begin
  131.     GetRealSegment:=PTR(GetSelector(RequiredSegment),0)
  132.   End;
  133.  
  134.  
  135.  
  136. Begin
  137.   SelectorValid:=False  {We have no selector yet!}
  138. End.
  139.  
  140.